/*---------------------------------------------------------

  Space Is The Place Gamemode

---------------------------------------------------------*/

--Two teams:
--	FSA (Free Space Authority), GMC (Galactic Mining Corporation)
-- Spawnpoints[Map][Team][point number]

SITP.SpawnPoints = {}
SITP.MapList = {}

--sb_twinsuns_fixed

SITP.SpawnPoints["sb_twinsuns_fixed"] = {}
SITP.SpawnPoints["sb_twinsuns_fixed"]["FSA"] = {}
SITP.SpawnPoints["sb_twinsuns_fixed"]["GMC"] = {}

SITP.SpawnPoints["sb_twinsuns_fixed"]["FSA"][1] = Vector(-9063.54, 12358, -8091)
SITP.SpawnPoints["sb_twinsuns_fixed"]["FSA"][2] = Vector(-8430.27, 12541.25, -8048.31)
SITP.SpawnPoints["sb_twinsuns_fixed"]["FSA"][3] = Vector(-8797.19, 12519.47, -8077.65)
SITP.SpawnPoints["sb_twinsuns_fixed"]["FSA"][4] = Vector(-8219.16, 12200.07, -8123.33)
SITP.SpawnPoints["sb_twinsuns_fixed"]["FSA"][5] = Vector(-8687.67, 12002.9, -8132.67)

SITP.SpawnPoints["sb_twinsuns_fixed"]["GMC"][1] = Vector(12012.6, -9101.6, -7988.6)
SITP.SpawnPoints["sb_twinsuns_fixed"]["GMC"][2] = Vector(11641.45, -10898.13, -8068.94)
SITP.SpawnPoints["sb_twinsuns_fixed"]["GMC"][3] = Vector(10402.23, -11336.8, -8001.7)
SITP.SpawnPoints["sb_twinsuns_fixed"]["GMC"][4] = Vector(11166.4, -9271.7, -8087.46)
SITP.SpawnPoints["sb_twinsuns_fixed"]["GMC"][5] = Vector(9493.67, -10657.9, -8022.27)

table.insert( SITP.MapList, "sb_twinsuns_fixed" )

-- gm_new_worlds

SITP.SpawnPoints["gm_new_worlds"] = {}
SITP.SpawnPoints["gm_new_worlds"]["FSA"] = {}
SITP.SpawnPoints["gm_new_worlds"]["GMC"] = {}

SITP.SpawnPoints["gm_new_worlds"]["FSA"][1] = Vector(9573.71, -6291, -7572)
SITP.SpawnPoints["gm_new_worlds"]["FSA"][2] = Vector(8709, -6796, -7515)
SITP.SpawnPoints["gm_new_worlds"]["FSA"][3] = Vector(8247, -6875, -7543)
SITP.SpawnPoints["gm_new_worlds"]["FSA"][4] = Vector(8321, -6160, -7550)
SITP.SpawnPoints["gm_new_worlds"]["FSA"][5] = Vector(8357, -5526, -7563)

SITP.SpawnPoints["gm_new_worlds"]["GMC"][1] = Vector(-13459, 7087, 3168)
SITP.SpawnPoints["gm_new_worlds"]["GMC"][2] = Vector(-13399, 7574, 3184)
SITP.SpawnPoints["gm_new_worlds"]["GMC"][3] = Vector(-13313, 7868, 3184)
SITP.SpawnPoints["gm_new_worlds"]["GMC"][4] = Vector(-13347, 8269, 3190)
SITP.SpawnPoints["gm_new_worlds"]["GMC"][5] = Vector(-13145, 8491, 3190)

table.insert( SITP.MapList, "gm_new_worlds" )

-- Spawnpoints

function GetTeamCounts()
	local Teams = {}
	local allplys = player.GetAll()
	
	Teams.FSA = 0
	Teams.GMC = 0
	Teams.Merc = 0
	
	for k, v in pairs(allplys) do
		if(v.sitp_team == "merc") then
			Teams.Merc = Teams.Merc + 1
		elseif(v.sitp_team == "fsa") then
			Teams.FSA = Teams.FSA + 1
		elseif(v.sitp_team == "gmc") then
			Teams.GMC = Teams.GMC + 1
		end
	end
	
	return Teams
end

function GM:PlayerInitialSpawn( ply )
	ply.sitp_team = "merc"
	ply:SetNetworkedString("sitp_team", "merc")
	self.BaseClass:PlayerInitialSpawn( ply )
	
	umsg.Start( "SITPHelp", ply )
	umsg.End()
end

function GM:PlayerSpawn( ply )
	self.BaseClass:PlayerSpawn( ply )
	ply.sitp_team = ply:GetNetworkedString("sitp_team")
	if(table.HasValue(SITP.MapList, game.GetMap())) then
		local rnd = math.random(1, 5)
		local FSA = SITP.SpawnPoints[game.GetMap()]["FSA"][rnd]
		local GMC = SITP.SpawnPoints[game.GetMap()]["GMC"][rnd]
		
		if( ply.sitp_team == "fsa" ) then
			ply:SetPos(FSA)
		elseif( ply.sitp_team == "gmc" ) then
			ply:SetPos(GMC)
		end
	end
end

function GM:ShowTeam( ply )
	umsg.Start( "SITPTeam", ply )
	umsg.End()
end